local skel = fk.CreateSkill {
  name = "rmt__xuanshu",
}

skel:addEffect(fk.CardUseFinished, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target == player then
      return player:getHandcardNum() ~= 4 and data.card.type == Card.TypeBasic
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local prompt = player:getHandcardNum() > 4 and "discard" or "draw"
    prompt = "#rmt__xuanshu-"..prompt..":::"..data.card.trueName..":"..(player:usedSkillTimes(skel.name) + 1)
    local n = player:getHandcardNum() - 4
    if n > 0 then
      local cards = room:askToDiscard(player, {
        max_num = n, min_num = n, include_equip = false, cancelable = true, skill_name = skel.name, skip = true,
        prompt = prompt,
      })
      if #cards > 0 then
        event:setCostData(self, {cards = cards})
        return true
      end
    elseif room:askToSkillInvoke(player, {skill_name = skel.name, prompt = prompt}) then
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local x = player:getHandcardNum() - 4
    if x > 0 then
      room:throwCard(event:getCostData(self).cards, skel.name, player, player)
    else
      player:drawCards(-x, skel.name)
    end
    if not player.dead then
      room:setPlayerMark(player, "@rmt__xuanshu-turn", data.card.trueName)
    end
  end,
})

skel:addEffect(fk.CardUsing, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("@rmt__xuanshu-turn") ~= 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getMark("@rmt__xuanshu-turn")
    room:setPlayerMark(player, "@rmt__xuanshu-turn", 0)
    if mark ~= data.card.trueName then
      room:notifySkillInvoked(player, skel.name, "negative")
      local x = player:usedSkillTimes(skel.name)
      room:askToDiscard(player, {
        max_num = x, min_num = x, include_equip = true, cancelable = false, skill_name = skel.name
      })
    end
  end,
})

Fk:loadTranslationTable{
  ["rmt__xuanshu"] = "玄枢",
  [":rmt__xuanshu"] = "你使用基本牌后，可以调整手牌数至4，然后若你本回合使用的下一张牌牌名与之不同，你弃置X张牌。（X为此技能本回合发动次数）",

  ["#rmt__xuanshu-draw"] = "玄枢：你可以摸牌至4，若你本回合使用下一张牌不为【%arg】，你须弃%arg2张牌",
  ["#rmt__xuanshu-discard"] = "玄枢：你可以弃牌至4，若你本回合使用下一张牌不为【%arg】，你须弃%arg2张牌",
  ["@rmt__xuanshu-turn"] = "玄枢",

  ["$rmt__xuanshu1"] = "名无名，誉无誉，可拟圣人。",
  ["$rmt__xuanshu2"] = "无形、无声、无物、无我，谓之全。",
}

return skel
